<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Models as Models;

class VideoController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {

    }
    //上传页面
    public function uploadVideo(Request $request)
    {
        $file = $_FILES['files'];
        //ajax返回数组
        $data = array('sta'=>TRUE,'msg'=>'上传成功！');
        $ext = $this->getExt($file['name']);
        $arrExt = array('3gp','rmvb','flv','wmv','avi','mkv','mp4','mp3','wav');
        if(!in_array($ext,$arrExt)) {
            $data['sta'] = FALSE;
            $data['msg'] = '不支持此类型文件的上传！';
        }
        $previewPath = 'video';
        if($file['error'] == 0) {
            //文件上传到预览目录
            $previewName = 'pre_'.md5(mt_rand(1000,9999)).time().'.'.$ext;
            $previewSrc = $previewPath.$previewName;
            if(!move_uploaded_file($file['tmp_name'],$previewSrc)) {
                $data['sta'] = FALSE;
                $data['msg'] = '上传失败！';
            } else {
                $data['previewSrc'] = $previewSrc;
            }
            echo json_encode($data);
        }
    }
    //获取文件扩展名
    function getExt($filename) {
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
        return $ext;
    }
}
